function e(e) {
    if (e && e.__esModule) return e;
    var t = {};
    if (null != e) for (var n in e) Object.prototype.hasOwnProperty.call(e, n) && (t[n] = e[n]);
    return t.default = e, t;
}

function t(e) {
    return e && e.__esModule ? e : {
        default: e
    };
}

function n(e) {
    wx.showToast({
        title: e,
        image: "./bad-toast.png",
        duration: 3e3
    });
}

function a(e) {
    var t = null;
    return e.mobile ? e.verifycode || (t = "请输入验证码") : t = "请输入手机号", t && n(t), !t;
}

function i(e) {
    var t = new Promise(function(e, t) {
        wx.showLoading({
            title: "加载中..."
        }), wx.login({
            complete: wx.hideLoading,
            success: e,
            fail: t
        });
    });
    return new Promise(function(a, i) {
        t.then(function(t) {
            var n = t.code, a = getApp().globalData, i = a.app, o = a.appid, r = a.environment, s = f.apiPrefix + "/user/" + ("wx" === r ? "bindmobilev2" : "qywxbindmobile");
            return d.post(s, u({}, e, {
                app: i,
                appid: o,
                code: n
            }));
        }).then(function(e) {
            if (0 !== e.code) return n(e.message), Promise.reject(e);
            l.default.updateTokens(e.data), wx.showToast({
                title: "已登录"
            });
            r(e.data.openid, "bindmobile"), o();
        });
    });
}

function o() {
    var e = getCurrentPages(), t = e[e.length - 2], n = "/" + t.route + "?" + c.default.serializeParams(t.__query);
    2 === e.length && wx.reLaunch ? wx.reLaunch({
        url: n
    }) : wx.redirectTo({
        url: n
    });
}

function r(e, t) {
    var n = getApp().globalData.appid, a = f.apiPrefix + "/user/statisticslogin";
    return d.post(a, {
        appid: n,
        openid: e,
        op: t
    }).then(function(e) {
        console.log(e, "获取用户信息" + t);
    });
}

function s(e) {
    var t = arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : "1.9.90";
    e = e.split("."), t = t.split(".");
    for (var n = Math.max(e.length, t.length); e.length < n; ) e.push("0");
    for (;t.length < n; ) t.push("0");
    for (var a = 0; a < n; a++) {
        var i = parseInt(e[a], 10), o = parseInt(t[a], 10);
        if (i > o) return 1;
        if (i < o) return -1;
    }
    return 0;
}

var u = Object.assign || function(e) {
    for (var t = 1; t < arguments.length; t++) {
        var n = arguments[t];
        for (var a in n) Object.prototype.hasOwnProperty.call(n, a) && (e[a] = n[a]);
    }
    return e;
}, c = t(require("../../utils/util")), l = t(require("../../utils/user")), d = e(require("../../utils/request")), f = e(require("../../utils/host")), p = t(require("../../utils/api"));

Page({
    data: {
        isGetUserInfo: !1,
        isAllowUseOpenData: !1,
        source: "",
        tokens: {},
        useWechat: !0,
        binding: !1,
        mobile: "",
        countDown: 0,
        verifycode: ""
    },
    onLoad: function(e) {
        var t = this, n = e.source, a = s(wx.getSystemInfoSync().SDKVersion) > -1;
        l.default.getTokens().then(function(e) {
            t.setData({
                source: n,
                tokens: e,
                isAllowUseOpenData: a
            });
            r(e.openid, "authpage");
        });
    },
    getUserInfo: function(e) {
        if (!e.detail.errMsg.includes("getUserInfo:fail auth deny")) {
            var t = e.detail, n = t.userInfo, a = t.encryptedData, i = t.iv, o = n.nickName, s = n.avatarUrl, u = this.data.tokens;
            this.setData({
                "tokens.realname": o,
                "tokens.avatar": s,
                isGetUserInfo: !0
            }), wx.showToast({
                title: "授权成功"
            }), this.updateUserInfo(this, u, a, i);
            r(u.openid, "authuser");
        }
    },
    updateUserInfo: function(e, t, n, a) {
        wx.login({
            success: function(i) {
                var o = i.code, r = f.apiPrefix + "/user/authuser", s = getApp().globalData, u = s.app, c = s.appid;
                d.post(r, {
                    app: u,
                    appid: c,
                    code: o,
                    encryptedData: n,
                    iv: a
                }).then(function(n) {
                    var a = n.code, i = n.data;
                    0 === a && (t = Object.assign({}, t, i), e.setData({
                        tokens: t
                    }));
                });
            }
        });
    },
    signinByWechat: function(e) {
        var t = this, a = e.detail, o = a.errMsg, r = a.iv, s = a.encryptedData;
        o.includes("getPhoneNumber:fail") ? (n("快速获取失败"), this.setData({
            useWechat: !1
        })) : (this.setData({
            binding: !0
        }), i(u({
            iv: r,
            encryptedData: s
        }, this.data.tokens)).then(function() {
            t.setData({
                binding: !1
            });
        }, function(e) {
            console.log("Error in bindMobileByWechat: ", e), t.setData({
                useWechat: !1
            });
        }));
    },
    inputMobile: function(e) {
        var t = e.detail.value;
        this.setData({
            mobile: t
        });
    },
    getVerifycode: function() {
        var e = this, t = this.data, a = t.mobile, i = t.source;
        c.default.isMobile(a) ? (this.setData({
            countDown: 60
        }), this.updateCountDown(Date.now()), p.default.users.sendcode({
            mobile: a,
            source: i
        }).then(function(e) {
            if (wx.showToast({
                title: "验证码已发送"
            }), 0 !== e.code || !e.data.issend) return Promise.reject(e);
        }).catch(function(t) {
            n("短信发送失败"), e.setData({
                countDown: 0
            });
        })) : n("手机格式错误");
    },
    inputVerifycode: function(e) {
        var t = e.detail.value;
        this.setData({
            verifycode: t
        });
    },
    submit: function() {
        var e = this.data, t = e.mobile, n = e.verifycode, o = e.tokens;
        a({
            mobile: t,
            verifycode: n
        }) && i(u({
            mobile: t,
            verifycode: n
        }, o));
    },
    updateCountDown: function(e) {
        var t = this, n = Date.now(), a = this.data.countDown;
        a -= (n - e) / 1e3, this.setData({
            countDown: a
        }), a > 0 && setTimeout(function() {
            return t.updateCountDown(n);
        }, 60);
    }
});